% plot_memory_stats_from_csv.m
% Plot memory usage statistics from CSV data
function plot_memory_stats_from_csv(csv_file, output_dir, case_name)
    % Read CSV data
    data = readtable(csv_file);
    
    % Extract unique cache types
    cache_types = unique(data.cache_type);
    
    for ct = cache_types'
        % Filter data for current cache type
        type_data = data(data.cache_type == ct, :);
        
        if isempty(type_data)
            continue;
        end
        
        % Create figure
        fig = figure('Name', sprintf('Memory Stats - Cache Type %d', ct), ...
                     'Position', [100, 100, 1000, 600]);
        tiledlayout(3, 1);
        
        % Plot 1: Memory Usage
        ax1 = nexttile;
        plot(ax1, type_data.step, type_data.usage_percent, 'b-', 'LineWidth', 1.5);
        title('Memory Usage (%)');
        xlabel('Step');
        ylabel('Usage (%)');
        grid on;
        ylim([0 100]);
        
        % Plot 2: Number of Free Blocks
        ax2 = nexttile;
        plot(ax2, type_data.step, type_data.num_free_blocks, 'r-', 'LineWidth', 1.5);
        title('Number of Free Blocks (Fragmentation)');
        xlabel('Step');
        ylabel('Free Block Count');
        grid on;
        
        % Plot 3: Max Free Block Size
        ax3 = nexttile;
        plot(ax3, type_data.step, type_data.max_free_block_size, 'g-', 'LineWidth', 1.5);
        title('Maximum Free Block Size');
        xlabel('Step');
        ylabel('Size (Bytes)');
        grid on;
        
        % Save figure
        filename = sprintf('%s_memory_stats_cache%d.png', case_name, ct);
        filepath = fullfile(output_dir, filename);
        saveas(fig, filepath);
        
        fprintf('  Memory stats plot saved: %s\n', filepath);
        close(fig);
    end
end



